package cn.edu.gzgs.controller;

import cn.edu.gzgs.dto.AuditContentDTO;
import cn.edu.gzgs.dto.AuditPageQueryDTO;
import cn.edu.gzgs.result.Result;
import cn.edu.gzgs.service.AuditService;
import cn.edu.gzgs.vo.WebOfficeVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "工作量申报接口", description = "工作量申报和佐证材料处理") // 类级标签
@RestController
@RequestMapping("/api/audit")
@Slf4j
public class AuditController {

    @Autowired
    private AuditService auditService;

    /**
     * 审核内容新增
     * @param auditContentDTO 审核内容新增
     * @return Result
     */
    @Operation(summary="审核内容新增和佐证材料上传")
    @Parameter(name = "auditContentDTO", description = "审核内容和佐证材料列表", required = true)
    @PostMapping("/add")
    public Result AuditAdd(@RequestBody AuditContentDTO auditContentDTO) {
        log.info("审核内容新增/编辑DTO: {}", auditContentDTO);
        //添加审核内容和佐证材料列表
        auditService.auditWithsupportingMLAdd(auditContentDTO);
        return Result.success();
    }

    /**
     * 动态查询审核内容
     * @param auditPageQueryDTO
     * @return PageDTO
     */
    @PostMapping("/queries")
    @Operation(summary = "动态查询审核内容")
    @Parameter(name = "auditPageQueryDTO", description = "审核查询参数", required = true)
    public PageDTO<AuditContentDTO> dynamicQueries(@RequestBody AuditPageQueryDTO auditPageQueryDTO) {
        log.info("查询内容：{}", auditPageQueryDTO);
        // 获取查询参数
        PageDTO<AuditContentDTO> pageDTO =auditService.dynamicQueries(auditPageQueryDTO);
        return pageDTO;
    }



    /**
     * 修改审核内容
     * @param auditContentDTO
     * @return Result
     */
    @Operation(summary="修改审核内容")
    @Parameter(name = "auditContentDTO", description = "审核内容DTO", required = true)
    @PutMapping("/update")
    public Result update(@RequestBody AuditContentDTO auditContentDTO) {
        log.info("修改审核内容DTO: {}", auditContentDTO);
        auditService.update(auditContentDTO);
        return Result.success();
    }

    /**
     * 在线查看PDF文件
     * @param materialId 佐证材料ID
     * @param expireInSeconds URL有效期，单位秒，默认30分钟
     * @return 签名URL
     */
    @Operation(summary = "在线查看PDF文件")
    @Parameter(name = "materialId", description = "佐证材料ID", required = true)
    @GetMapping("/view-pdf/{materialId}")
    public Result<WebOfficeVO> viewPdfFile(
            @PathVariable Long materialId,
            @RequestParam(defaultValue = "1800") Long expireInSeconds) throws Exception {
        log.info("在线查看PDF文件，材料ID：{}，有效期：{}", materialId, expireInSeconds);
        WebOfficeVO signedUrl = auditService.getSupportingMaterialSignedUrl(materialId, expireInSeconds);
        return Result.success(signedUrl);
    }


    /**
     * 撤销审核内容
     * @param id
     */
    @Operation(summary = "撤销审核内容")
    @Parameter(name = "id", description = "审核内容ID", required = true)
    @DeleteMapping("/revoke/{id}")
    public Result revoke(@PathVariable Long id) {
        log.info("撤销审核内容，ID：{}", id);
        auditService.revokeAudit(id);
        return Result.success();
    }

}
